package com.rado.tf.gui.view.menu.impl;

import com.rado.tf.gui.i18n.MessageSource;
import com.rado.tf.gui.navigation.MenuEntries;
import com.rado.tf.gui.view.common.impl.AbstractViewImpl;
import com.rado.tf.gui.view.menu.IMenuView;
import com.rado.tf.gui.view.menu.MenuPlugin;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.themes.ChameleonTheme;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * Created by parado on 17.03.14.
 */
@Component(MenuViewImpl.BEAN_NAME)
public class MenuViewImpl extends AbstractViewImpl implements IMenuView
{
   public static final String BEAN_NAME = "menuView";

   @Resource
   private MessageSource messageSource;

   @Resource
   private MenuPlugin menuPlugin;

   private CssLayout root;

   private Table table;

   @Override
   @PostConstruct
   public void init()
   {
      root = new CssLayout();
      root.setSizeFull();
      root.setStyleName("sidebar-menu");
      root.addComponent(new Label(messageSource.getMessage("menu.header")));

      table = new Table();
      table.setSizeFull();
      table.setNullSelectionAllowed(false);
      table.setColumnHeaderMode(Table.ColumnHeaderMode.HIDDEN);
      table.setSelectable(true);
      table.addStyleName(ChameleonTheme.TABLE_BORDERLESS);
      table.addContainerProperty("menu-column", String.class, "");

      for (String entry : MenuEntries.entries)
      {
         table.addItem(menuPlugin.menuItem(entry), entry);
      }

      root.addComponent(table);
      addComponent(root);
   }

   @Override
   public void addMenuItemClickListener(ItemClickEvent.ItemClickListener listener)
   {
      table.addItemClickListener(listener);
   }

   @Override
   public void selectMenuItem(String itemId)
   {
      table.select(itemId);
   }
}
